package studyguide.ch09.customitem;

import javax.microedition.lcdui.CustomItem;
import javax.microedition.lcdui.Graphics;

public class CustomItemA extends CustomItem {

	// The preferred area is greater than the minimum area:
	private static final int MIN_COMPONENT_WIDTH = 70;
	private static final int MIN_COMPONENT_HEIGHT = 20;
	private static final int PREFERRED_COMPONENT_AREA = 2000;
	
	private final int borderColor; 
	
	protected CustomItemA(String label, int borderColor) {
		super(label);
		this.borderColor = borderColor;
	}

	protected int getMinContentHeight() {
		return MIN_COMPONENT_HEIGHT;
	}

	protected int getMinContentWidth() {
		return MIN_COMPONENT_WIDTH;
	}

	protected int getPrefContentWidth(int height) {
		System.out.println("[" + getLabel() + "]getPrefContentWidth(" + height + ")");
		
		int width;
		if (height == -1) {
			// Default width
			width = MIN_COMPONENT_WIDTH;
		} else {
			width = PREFERRED_COMPONENT_AREA / height;
		}
		return width;
	}

	protected int getPrefContentHeight(int width) {
		System.out.println("[" + getLabel() + "]getPrefContentHeight(" + width + ")");
		
		int height;
		if (width == -1) {
			// Default heigh
			height = MIN_COMPONENT_HEIGHT;
		} else {
			height = PREFERRED_COMPONENT_AREA / width;
		}
		return height;
	}
	
	protected void paint(Graphics g, int w, int h) {
		// Clear screen:
		g.setColor(0x00ffffff);// White
		g.fillRect(0, 0, w, h);
		
		// Draw a border:
		g.setColor(borderColor);
		g.drawRect(0, 0, w-1, h-1);
	}

}
